﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Common;

namespace Model
{
    public class NewsDAO
    {
        public DataTable GetTopNews(int numLimit)
        {
            string whereClause1 = "<Leq><FieldRef Name=\"FromDate\" /><Value Type=\"DateTime\"><Today/></Value></Leq>";
            string whereClause2 = "<Geq><FieldRef Name=\"ToDate\" /><Value Type=\"DateTime\"><Today/></Value></Geq>";

            string whereClause = string.Format("<And>{0}{1}</And>", whereClause1, whereClause2);
            string orderClause = "<FieldRef Name=\"FromDate\" Ascending=\"True\" />";

            string strCamlQuery = "<Where>{0}</Where><OrderBy>{1}</OrderBy>";
            return DataAccess.GetTableFromCaml
                (string.Format(strCamlQuery, whereClause, orderClause)
            , "News", uint.Parse(numLimit.ToString()), new string[] {"ID", "Title", "Description", "ImageLink" });
        }

        public DataTable GetAllNews()
        {
            string whereClause = "";
            string strCamlQuery = "<Where>{0}</Where><OrderBy>{1}</OrderBy>";
            string orderClause = "<FieldRef Name=\"FromDate\" Ascending=\"True\" />";

            return DataAccess.GetTableFromCaml
                (string.Format(strCamlQuery,whereClause,orderClause)
            , "News", new string[] { "ID", "Title", "Description", "ImageLink" });
        }
    }
}
